<?php
if (count($this->taskView->tasks) > 0) :
    foreach ($this->taskView->tasks as $task):
        $signal = $task->signal;
        $messageTags = $signal->findTag();
        $timestamp = $signal->date->format("U");
        $status = $task->status;
        $completeText = "";
        $completeAction = "Mark Complete";
        $user1 = User::withId($task->taskUserId);
        $user2 = User::withId($task->taskAssignedTo);
        $message = $signal->message;
        $dateCreated = $task->taskDateCreated;
        $stream = Stream::withId(base64_encode($signal->streamID));

        $date = new DateTime($message->getTime());
        $timestamp = $date->format("U");
        $twitter = Twitter::getTwitters();
        $facebook = Facebook::getFacebooks();
        $google = Google::getGoogles();
        $linkedin = LinkedIn::getLinkedIns();
        if ($status == 2) {
            $completeText = "completed";
            $completeAction = "Undo Complete";
        }
        $videoId = '';
        if ($message->getSource() == 'www.youtube.com') { //Parse youtube links
            $url = $message->getURL();
            $urlParams = explode('?v=', $url);
            $videoIdTemp = $urlParams[1];
            $idParams = explode('&', $videoIdTemp);
            $videoId = $idParams[0];
            $embedUrl = "https://youtube.googleapis.com/v/" . $videoId;
        }
        ?>
        <li id="<?php echo $task->taskId; ?>" class="task <?php echo $completeText; ?>" threadid="<?php echo $message->getThreadId(); ?>" messageid="<?php echo $signal->messageID; ?>" streamid="<?php echo $signal->streamID; ?>">
            <div class="mention-head" replies="">
                <div>
                    <img class="social" src="Images/icon-<?php echo StreamController::getSMTIcon($message->getType()); ?>.png" alt="">
                    <a class="title" text="<?php echo $message->getThreadTitle(); ?>" href="<?php echo $message->getURL(); ?>" target="_blank">
                        <?php echo $message->getThreadTitle(); ?>
                    </a>
                </div>

                <div class="info">
                    <img src="http://www.google.com/s2/favicons?domain=<?php echo $message->getSource(); ?>" alt="">
                    <p class="link"><span class="source" text="<?php
                        $parse = parse_url($message->getURL());
                        $parsedSource = str_ireplace('www.', '', $parse["host"]);
                        echo $parsedSource;
                        ?>"><?php echo $parsedSource; ?></span><span>|</span><span class="author" text="<?php echo $message->getLeadAuthor(); ?>"><?php echo $message->getLeadAuthor(); ?></span></p>
                    <span class="social">
                        <img src="Images/comment.png" alt=""> <p><?php echo $totalReplies ?></p> &nbsp;
                        <img src="Images/like.png" alt=""> <p><?php echo $message->getNumberOfLikes() ?></p> &nbsp;
                    </span>
                    <p class="date-time" data-timestamp="<?php echo $timestamp; ?>" data-timestamp-format="MMM D YYYY h:mm A"><?php echo $message->getTime(); ?></p>
                </div>

                <div class="content" text='<?php
                $text = trim(rtrim($signal->text, 'null'));
                $text = str_replace('(javascript:void(0))', '', $text);
                $text = str_replace("\n", "<br>", $text);
                eregi("<blockquote>(.*)</blockquote>", $text, $data);
                $blockQuote = $data[0];
                $text = str_replace($blockQuote, "{blockquote}", $text);
                $threadId = $message->getThreadId();
                $data = Sentiment::saveSentiment(Signal::removeLink($text), $masterView->streamView->model->streamId, $messageId, $threadId, $message->getTime(), $message->getType(), $message->getSource());
                $sentimentScore = $data["score"];
                $sentimentText = $data["text"];
                $sentimentColor = $data["color"];
                echo Signal::parseLinks(str_replace("{blockquote}", $blockQuote, $text));
                ?>'>
                         <?php
                         if (substr_count($text, " ") > 100) {
                             //find the 100th char(' ')
                             $endpos = 120;
                             $endpos = strpos($text, " ", $endpos);
                             $shortText = substr($text, 0, $endpos);
                             echo Signal::parseLinks(str_replace("{blockquote}", $blockQuote, $shortText));
                             ?>
                        <span class="readmore-threedot">...</span>
                        <?php
                    } else {
                        echo Signal::parseLinks(str_replace("{blockquote}", $blockQuote, $text));
                    }
                    ?>
                </div>
                <div class="tag-bar hide">
                    <div class="tag-zone tag-zone-hide" id="tag-zone">
                        <div class="tag threedot hide"><div class="name"><i class="fa fa-sort"></i></div></div>
                        <?php
                        foreach ($signal->findTag() as $tag) {
                            echo '<div class="tag"><div class="name">#' . $tag->tagName . '</div><div class="del">x</div></div>';
                        }
                        ?>
                    </div>
                </div>
            </div>
            <div class="buzz buzz-task hide">
                <div class="task-info">
                    <p class="announce">
                        <img src="http://www.gravatar.com/avatar/<?php echo md5(strtolower($user1->email)); ?>?d=http://pulse.buzzmetrics.vn/Images/pulse_75.png">
                        <span class="name"><?php echo $user1->name ?></span>
                        assigned a task to 
                        <img src="http://www.gravatar.com/avatar/<?php echo md5(strtolower($user2->email)); ?>?d=http://pulse.buzzmetrics.vn/Images/pulse_75.png">
                        <span class="name"><?php echo $user2->name ?></span>
                    </p>
                    <p class="time" data-timestamp="<?php echo $timestamp ?>" data-timestamp-format="MMM D YYYY h:mm A"><?php echo $dateCreated->format("M d Y h:m A") ?></p>
                </div>
                <div class="task-detail">
                    <i class="fa fa-task fa-3x" style="float: left; color: #eee; margin-right: 5px"></i>
                    <p><strong><?php echo $task->taskName; ?></strong></p>
                    <p class="task-note" text="<?php echo $task->taskNote; ?>"><?php
                        if ($completeText)
                            echo ucfirst($completeText) . " '" . $task->taskNote . "' with solution '" . $task->action . "'";
                        else
                            echo $task->taskNote;
                        ?></p> <br />
                </div>
                <div class="action">
                    <p>
                        <span class="mark-complete"><i class="fa fa-check"></i>
                            <?php echo $completeAction; ?> &nbsp; &nbsp;</span>
                        <span class="delete <?php if ($completeText) echo 'hide'; ?>"><i class="fa fa-trash-o"></i></span>
                    </p>
                </div>
            </div>
        </li>
        <?php
    endforeach;
else:
    ?>
    <div class="no-result" style="width: 300px;font-size: 13pt;text-align: center;padding: 30px 10px;vertical-align: middle;margin: 150px auto;background: #FFF;border: 1px solid #BBB;border-radius: 2px;box-shadow: 1px 1px 1px #BBB;">
        <h1 style="line-height: 70pt; font-size: 49pt;-webkit-transform: rotate(90deg);-moz-transform: rotate(90deg);-o-transform: rotate(90deg);transform: rotate(90deg);">:-(</h1>
        <h3>Sorry!</h3>
        There's no data yet.<br>Please check back later...
    </div>
<?php endif; ?>